<?php
namespace App\Libs\Common;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 *   京东签约代扣异步通知
 */
class JdDkYbController extends Controller
{	


    public function _initialize()
    {   
        header("Content-type: text/html; charset=utf-8");
        
        vendor("jdPay_dk.NotifyMock");
    }

    /**
     * 签约结果异步通知
     */
    public function signNotify()
    {
        
        $return_content=file_get_contents("php://input");

        $obj = new \NotifyMock();

        $result=$obj->signNotify($return_content);

        //协议生效
        if ($result['agreement_status'] == 'FINI') 
        {   
            $row=M('orders')->where("order_id='$result[out_trade_no]'")->field("goods_id,user_phone,order_status")->find();
            if ($row["order_status"] == 0) 
            {
                //商品浏览量统计 
                goods_statia($row["goods_id"],$row["user_phone"],2);
            }

            //  更改订单状态
            $saveOrder=M('orders')->where("order_id='$result[out_trade_no]' and order_status=0")->save(array('order_status'=>1,'zm_order_no'=>$result['agreement_no']));
           
            $time=date("Y-m-d H:i:s",time());
            // 修改我的优惠券表 使用状态 使用时间
            $my_cou_id=M("orders")->where("order_id='$result[out_trade_no]'")->getField("my_cou_id");
            if($my_cou_id != 0)
            {
               $cou_coupon_res=M("cou_coupon")->where("id='$my_cou_id'")->save(array("my_cou_status"=>2,"use_time"=>$time)); 
            }
        }

        //协议关闭
        if ($result['agreement_status'] == 'CLOS') 
        {
            $res=M("orders")->where("order_id='$result[out_trade_no]' and order_status > 0 and zm_order_no != ''")->save(array("zm_order_no"=>''));
        }
    }


    /**
     * 代扣异步通知
     */
    public function tradeNotify()
    {  
        $time=date('Y-m-d H:i:s');

        $return_content=file_get_contents("php://input");
        
        $obj = new \NotifyMock();

        $res=$obj->tradeNotify($return_content);
     
        if ($res["payStatus"]) 
        {   
            $out_trade_no=$res['data']['out_trade_no'];
            
            save_order_stages($out_trade_no,$res['data']['trade_amount']/100);

            $dk_status=M('pay_dk_bill')->where("stages_id=$out_trade_no")->getField("dk_status");
           
            if ($dk_status == 0 || $dk_status == 1)
            {
                M('pay_dk_bill')->where("stages_id=$out_trade_no and dk_status in(0,1)")->save(array('dk_status'=>2,'last_time'=>$time,'keyword'=>$res['msg']));
            }   
        }

    }


 


	
}